package ywpt.web.action.config;

import java.util.ArrayList;
import java.util.List;

import org.ecside.table.limit.Limit;
import org.ecside.util.RequestUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import ywpt.core.exception.UniqueConstraintException;
import ywpt.core.web.BaseAction;
import ywpt.model.Department;
import ywpt.model.PeopleInfo;
import ywpt.model.PositionInfo;
import ywpt.service.config.DepartmentService;
import ywpt.service.config.PositionService;
import ywpt.service.people.PeopleService;
import ywpt.web.common.CommonDictionary;

import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;

public class PositionAction extends BaseAction implements
ModelDriven<PositionInfo>, Preparable {

	/**
	 * 职位信息Action
	 */
	private static final long serialVersionUID = 6790560611562739000L;
	private static int DEFAULT_PAGE_SIZE = 20;
	
	private Long id;
	private Long departId;
	private String departName;
	private PositionInfo positionInfo;
	private PositionService  positionService;
	private List<PositionInfo> positionInfoList = new ArrayList<PositionInfo>();
	
	private DepartmentService departmentService;
	private List<Department> departmentList = new ArrayList<Department>();
	
	private PeopleService peopleService;
	
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public PositionInfo getPositionInfo() {
		return positionInfo;
	}

	public void setPositionInfo(PositionInfo positionInfo) {
		this.positionInfo = positionInfo;
	}

	public PositionService getPositionService() {
		return positionService;
	}

	public void setPositionService(PositionService positionService) {
		this.positionService = positionService;
	}

	public List<PositionInfo> getPositionInfoList() {
		return positionInfoList;
	}

	public void setPositionInfoList(List<PositionInfo> positionInfoList) {
		this.positionInfoList = positionInfoList;
	}

	public PeopleService getPeopleService() {
		return peopleService;
	}

	public void setPeopleService(PeopleService peopleService) {
		this.peopleService = peopleService;
	}
	public Long getDepartId() {
		return departId;
	}

	public void setDepartId(Long departId) {
		this.departId = departId;
	}

	public String getDepartName() {
		return departName;
	}

	public void setDepartName(String departName) {
		this.departName = departName;
	}
	//*****************************************************
	@Override
	public PositionInfo getModel() {
		return this.positionInfo;
	}
	@Override
	public void prepare() throws Exception {
		if(id!=null){
			this.positionInfo = positionService.getPositionInfoById(id);
		}else{
			this.positionInfo = new PositionInfo();
		}
		
	}
//*****************************************************
	
	public String grouplist(){
		departmentList.addAll(positionService.getDepartments());
		return SUCCESS;
	}
	
	
	public String list() {
		DetachedCriteria query = DetachedCriteria.forClass(PositionInfo.class);
			query.add(Restrictions.eq("depart", departmentService.getDepartmentById(departId)));
		if(this.getDepartId()!=null)
		{
			query.createAlias("depart","depart").add(
					Restrictions.eq("depart.id",this.departId));
		}		
		int totalRows = RequestUtils.getTotalRowsFromRequest(getRequest());
		if (totalRows < 0) {
			totalRows = positionService.getPositionInfoCountByCriteria(query);
		}
		int[] rowStartEnd = RequestUtils.getRowStartEnd(getRequest(),
				totalRows, DEFAULT_PAGE_SIZE, 0);
		Limit limit = RequestUtils.getLimit(getRequest(), "ec", totalRows,
				DEFAULT_PAGE_SIZE);
		int pageSize = limit.getCurrentRowsDisplayed();
		query.addOrder(Order.asc("id"));
		query.setProjection(null);
		positionInfoList.addAll(positionService.getPositionInfos(query, pageSize,
				rowStartEnd[0]));
		getRequest().setAttribute("PAYMETHOD_MAP", CommonDictionary.PAYMETHOD);
		return SUCCESS;
	}

	public String input() 
	{
		String positionCode =this.positionService.createPositionCode();
		positionInfo.setPositionCode(positionCode);
		if(departId != null){
			this.departName = departmentService.getDepartmentById(departId).getDepartName();
		}
		return INPUT;
	}
	
	public String edit() 
	{
		this.positionInfo=this.positionService.getPositionInfoById(id);
		if(positionInfo.getDepart()!=null){
			this.setDepartId(positionInfo.getDepart().getId());
			this.setDepartName(positionInfo.getDepart().getDepartName());
		}
		return INPUT;
	}
	
	/**
	 * 保存
	 * 
	 * @return
	 */
	public String save() throws Exception 
	{
		try 
		{
			if (id == null) 
			{
				positionService.savePositionInfo(positionInfo, departId);
				this.addActionMessage("职位信息新增成功!");
			} 
			else 
			{
				positionService.updatePositionInfo(positionInfo, departId);
				this.addActionMessage("职位信息更新成功!");
			}

			return SUCCESS;
		} 
		catch (UniqueConstraintException e) 
		{

			this.addActionError("保存错误:" + e.getMessage());
			return INPUT;
		}
	}
	
	/**
	 * 删除
	 * 
	 * @return
	 */
	public String delete() throws Exception {
		if (id != null) 
		{	
			DetachedCriteria peopleQuery = DetachedCriteria.forClass(PeopleInfo.class);
			peopleQuery.add(Restrictions.eq("position", positionService.getPositionInfoById(id)));
			int	totalRows = peopleService.getPeopleInfoCountByCriteria(peopleQuery);
			if(totalRows>0)
			{
				this.addActionMessage("该职位已经发生过业务逻辑，不能删除！");
				return INPUT;
			}else{
				positionService.removePositionInfo(id);
				return SUCCESS;				
			}

		}
		this.addActionMessage("编号为空，请选择具体的记录！");
		return INPUT;
	}
	
	
	public String select() 
	{
		positionInfoList.addAll(positionService.getPositionInfos());
		return SUCCESS;
	}

	public List<Department> getDepartmentList() {
		return departmentList;
	}

	public void setDepartmentList(List<Department> departmentList) {
		this.departmentList = departmentList;
	}

	public DepartmentService getDepartmentService() {
		return departmentService;
	}

	public void setDepartmentService(DepartmentService departmentService) {
		this.departmentService = departmentService;
	}
	
}
